package com.jh.fcsm.util;

import com.jh.fcsm.util.txt.StringUtils;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

import java.util.Arrays;
import java.util.List;

/**
 * Example 查询条件工具
 *
 * @author szx
 */
public class ExampleUtil {

    public static final String RLIKE = "%";
    public static final String LIKE = "%%";
    public static final String EQUAL = "=";
    public static final String NON_EQUAL = "!=";
    public static final String GREATER_EQUAL = ">=";
    public static final String LESS_EQUAL = "<=";
    public static final String IN = "in";
    public static final String ORDER_ASC = "asc";
    public static final String ORDER_DESC = "desc";
    public static final String BETWEEN = "between";

    /**
     * 查询条件操作方法
     *
     * @param criteria {@link Criteria}
     * @param name     字段名称
     * @param value    字段值
     * @param type     查询条件符号类型
     */
    public static void and(Criteria criteria, String name, Object value, String type) {
        if (value != null && !"".equals(value)) {
            switch (type) {
                case RLIKE:
                    criteria.andLike(name, value + RLIKE);
                    break;
                case LIKE:
                    criteria.andLike(name, LIKE + value + LIKE);
                    break;
                case EQUAL:
                    criteria.andEqualTo(name, value);
                    break;
                case NON_EQUAL:
                    criteria.andNotEqualTo(name, value);
                    break;
                case GREATER_EQUAL:
                    criteria.andGreaterThanOrEqualTo(name, value);
                    break;
                case LESS_EQUAL:
                    criteria.andLessThanOrEqualTo(name, value);
                    break;
                case IN:
                    if (value instanceof List) {
                        criteria.andIn(name, (List<?>) value);
                    } else {
                        List<String> idList = Arrays.asList(String.valueOf(value).split(","));
                        criteria.andIn(name, idList);
                    }
                    break;
                case BETWEEN:
                    // 日期处理，后续需要优化
                    if (value instanceof List) {
                        List<?> list = (List<?>) value;
                        criteria.andBetween(name, list.get(0), list.get(1));
                    }
                    break;
                default:
                    break;
            }
        }

    }

    /**
     * 排序
     *
     * @param example   通用的Example查询对象
     * @param sortField 排序字段
     * @param sortType  排序规则
     */
    public static void orderBy(Example example, String sortField, String sortType) {
        if (!StringUtils.isEmpty(sortField)) {
            if (!StringUtils.isEmpty(sortType)) {
                if (ORDER_ASC.equals(sortType)) {
                    example.orderBy(sortField).asc();
                } else {
                    example.orderBy(sortField).desc();
                }
            }
        }
    }
}
